﻿using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using Simple.Repository.Models.ClassRoom;
using Image = System.Drawing.Image;

namespace Simple.Services.System.ClassRoom
{
    public class GardenHuoseService : BusinessBaseService
    {
        private readonly SimpleDbContext _context;
        private readonly CacheService _cacheService;

        public GardenHuoseService(SimpleDbContext context,
                           ISimpleService services,
                           CacheService cacheService) : base(services)
        {
            _context = context;
            _cacheService = cacheService;
        }

        public async Task<GardenHuoseOutputModel> GetGardenHuoseAsync()
        {
            var gardenHuoseInfo = await (await OrganizationFilter(_context.Set<GardenHouseInfo>())).FirstOrDefaultAsync();
            GardenHuoseOutputModel outResult =null;
            if (gardenHuoseInfo == null)
            {
                //throw AppResultException.Status409Conflict("未找到园所相关数据!");
                outResult = new GardenHuoseOutputModel();
            }
            else
                outResult = MapperHelper.Map<GardenHuoseOutputModel>(gardenHuoseInfo);
            return outResult;
        }

        public async Task<int> SaveGardenHuoseAsync(GardenHuoseOutputModel model)
        {
            var garden = await (await OrganizationFilter(_context.Set<GardenHouseInfo>())).FirstOrDefaultAsync();
            if (garden == null && !model.Id.HasValue)
            {
                //新增
                garden = MapperHelper.Map<GardenHouseInfo>(model);
                await _context.AddAsync(garden);
            }
            else if (model.Id.HasValue)
            {
                if (model.Id != garden?.Id)
                {
                    throw AppResultException.Status409Conflict("园所Id值错误,保存失败!");
                }

                MapperHelper.Map(model, garden);
                _context.Update(garden);
            }

            return await _context.SaveChangesAsync();
        }

        public async Task<string> GetGardenHuoseQrCode(string content, int outWidth, int outHeight)
        {
            var gardenHuoseInfo = await (await OrganizationFilter(_context.Set<GardenHouseInfo>())).FirstOrDefaultAsync();
            Image? logo = null;
            if (gardenHuoseInfo != null && !string.IsNullOrWhiteSpace(gardenHuoseInfo.GardenLogo))
            {
                //gardenHuoseInfo.GardenLogo = "";
                var logoImageBytes = Convert.FromBase64String(gardenHuoseInfo.GardenLogo.Replace("data:image/jpeg;base64,", string.Empty));
                logo = Image.FromStream(new MemoryStream(logoImageBytes));
            }

            QrCodeHelper.GenerateQRCode(content, outWidth, outHeight, imgFrt: ImageFormat.Jpeg, out var imageBase64String, logo);
            return imageBase64String;
        }


        public async Task<ProvinceCityDto> GetProvincesByLocationFile()
        {
            var provincefile = $"{AppDomain.CurrentDomain.BaseDirectory}city.json";
            if (string.IsNullOrWhiteSpace(provincefile) || !File.Exists(provincefile))
            {
                throw AppResultException.Status409Conflict("文件不存在!");
            }

            byte[] fileBytes = null;
            using (var fs = File.OpenRead(provincefile))
            {
                fileBytes = new byte[fs.Length];

                await fs.ReadAsync(fileBytes, 0, fileBytes.Length);
            }
            var data = new ProvinceCityDto();
            var provinceJsonStringData = Encoding.UTF8.GetString(fileBytes);
            if (!string.IsNullOrWhiteSpace(provinceJsonStringData))
            {
                try
                {
                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<ProvinceCityDto>(provinceJsonStringData);
                }
                catch
                {

                }
            }

            return data;
        }
    }
}
